#include "Header_xc8.h"
#include "_lcd.h"

#define lcd_en PORTBbits.RB3
#define lcd_rs PORTBbits.RB2

void init_lcd(void)
{
    __delay_ms(7);

    PORTB=0x30;
    lcd_rs=0;
    lcd_en=1;
    __delay_us(1);
    lcd_en=0;
    __delay_us(2293);

    PORTB=0x30;
    lcd_rs=0;
    lcd_en=1;
    __delay_us(1);
    lcd_en=0;
    __delay_us(2293);

    PORTB=0x30;
    lcd_rs=0;
    lcd_en=1;
    __delay_us(1);
    lcd_en=0;
    __delay_us(2293);

    PORTB=0x20;
    lcd_rs=0;
    lcd_en=1;
    __delay_us(1);
    lcd_en=0;
    __delay_us(2293);

    lcd_send_cmd((unsigned char)0x28);

    //lcd_send_cmd((unsigned char)0x10);

    lcd_send_cmd((unsigned char)0x01);

    lcd_send_cmd((unsigned char)0x0F);

    lcd_send_cmd((unsigned char)0x80);

    __delay_us(2293);
}

void lcd_send_cmd(unsigned char buf)
{
    unsigned char t;
    t = buf & 0xF0;
    PORTB = t;
    lcd_rs = 0;
    lcd_en = 1;
    __delay_us(1);
    lcd_en = 0;
    t = (buf << 4) & 0xF0;
    PORTB = t;
    lcd_rs=0;
    lcd_en = 1;
    __delay_us(1);
    lcd_en = 0;
    __delay_us(2293);
}

void lcd_send_data(unsigned char buf)
{
    unsigned char t;
    t = buf & 0xF0;
    PORTB = t;
    lcd_rs = 1;
    lcd_en = 1;
    __delay_us(1);
    lcd_en = 0;
    t = (buf << 4) & 0xF0;
    PORTB = t;
    lcd_rs = 1;
    lcd_en = 1;
    __delay_us(1);
    lcd_en = 0;
    __delay_us(2293);
}

void line_change(void)
{
	lcd_send_cmd((unsigned char)0xC0);
}
